home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3157 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: crl.crl.com!not-for-mail
  2. From: bobfry@crl.com (Robert Fry)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: performance of fread
  5. Date: 26 Jan 1996 08:22:35 -0800
  6. Organization: CRL Dialup Internet Access
  7. Message-ID: <4eav4b$ln7@crl.crl.com>
  8. References: <4eaecp$m5a@zeus.rbi.informatik.uni-frankfurt.de>
  9. NNTP-Posting-Host: crl.com
  10.  
  11. Ferber@zoology.uni-frankfurt.de (Michael Ferber) writes:
  12.  
  13. <description of problem snipped to conserve bandwidth>
  14.  
  15. >I think there are several ways to do this.
  16. >1. To read each integer and put it into the appropriate array, and the read   
  17. >   the next value. This assumes that I have call fread d * n times. Due to the 
  18. >   fact that I have to read many (several thousand) integers this seems to be 
  19. >   slow but easy to implement.
  20. >2. To read many integers into a buffer. And then copy the integers to their   
  21. >   appropriate arrays. This seems to be faster, but more difficult to control 
  22. >   compared with 3 and 1, because I always have to check if a new read is     
  23. > necessary while processing the data within the array.
  24. >3. To use the approach described in 1. but with a large buffer set with       
  25. >   setvbuf.
  26. >Furthermore I cannot read all the data of the file in a single turn because a 
  27. >single file may contain a theoretically unlimited number of integers, usually 
  28. >my files contain about 12 - 15 million points.
  29.  
  30. All three of these methods should work. However, from my own background 
  31. (image processing), method #2 has been the most common choice.
  32.  
  33. >My question: How time consuming is the overhead of calling fread reading from 
  34. >a large buffer compared to a for( ) loop which takes his data from a memory 
  35. >block which was filled by a single call of fread. 
  36.  
  37. This is the reason method #2 has been the usual choice: the answer to 
  38. your question is extremely implementation-dependent. The only way to get 
  39. an answer for your setup is to either test it yourself or query the 
  40. manufacturer. In my own experience, the use of many fread's is measurably 
  41. slower than a tight loop you might write for yourself, but whether that 
  42. difference is enough for you to want to make the extra effort is 
  43. something you need to decide for your situation.
  44.  
  45.   Bob
  46.